package com.gxc.mall.user.service.impl;

import com.gxc.mall.user.entity.UserEntity;
import com.gxc.mall.user.manager.UserManager;
import com.gxc.mall.user.repository.UserRepository;
import com.gxc.mall.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author GongXincheng
 * @since 2019-09-07 17:50
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Resource
    private UserManager userManager;

    @Override
    public UserEntity findUserById(Integer id) {
        return userRepository.findById(id).orElse(null);
    }


    @Override
    public void saveUser(UserEntity userEntity) {
//        userRepository.save(userEntity);
//        int i = 1 / 0;
        this.saveUserService(userEntity);
    }


    @Override
    @Transactional(rollbackOn = Exception.class)
    public void saveUserManager(UserEntity userEntity) {
        userEntity.setUsername("service层 user");
        userRepository.save(userEntity);
        userManager.addUser(userEntity);
        int i = 1 / 0;
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void updateUserTest(Integer id, String username) {
        userRepository.updateUserTest(id, username);
        int i = 1 / 0;
    }


    private void saveUserService(UserEntity userEntity) {
        userRepository.save(userEntity);
//        int i = 1 / 0;
    }


}
